package com.yuanjin.activity;

import java.io.File;
import java.io.FileNotFoundException;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.yuanjin.R;
import com.yuanjin.util.MediaUtils;
import com.yuanjin.util.MyTimeUtils;
import com.yuanjin.util.Yuanjin;

public class MyDetailInfoActivity extends Activity implements OnClickListener {
	private LinearLayout ll_headportrait;
	private ImageView headportrait;
	private SharedPreferences sp;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mydetail_info);
		initView();
	}

	private void initView() {
		headportrait = (ImageView) findViewById(R.id.headportrait);
		ll_headportrait = (LinearLayout) findViewById(R.id.ll_headportrait);

		headportrait.setOnClickListener(this);
		ll_headportrait.setOnClickListener(this);

		sp = getSharedPreferences(Yuanjin.YuanjinSharedPreference, MODE_PRIVATE);
		String thumbnailPath = sp.getString("headicon_thumbnailPath", "");
		// 若已设置头像则显示，否则显示默认头像
		if (thumbnailPath != "") {
			Bitmap bitmap = BitmapFactory.decodeFile(thumbnailPath);
			headportrait.setImageBitmap(bitmap);
		} else {
			headportrait.setImageResource(R.drawable.ic_default_userhead);
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.ll_headportrait:
			Intent intent = new Intent(Intent.ACTION_PICK, null);
			intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
					"image/*");
			startActivityForResult(intent, Yuanjin.ALBUM_OK);
			break;
		case R.id.headportrait:
			Intent intent2 = new Intent(MyDetailInfoActivity.this,
					HeadiconActivity.class);
			startActivity(intent2);
			break;
		default:
			break;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == Yuanjin.ALBUM_OK && data != null) {
			Uri uri = data.getData();
			Log.e("test", "ceshi-------------->> " + uri.toString());
			ContentResolver cr = this.getContentResolver();

			// 获得选中图片的绝对路径
			String absolutePath = MediaUtils.getImageAbsolutePath(
					MyDetailInfoActivity.this, uri);
			Log.e("MyDetailInfoActivity", "获得图片的绝对路径为 ：" + absolutePath);

			// 显示原图
			// Bitmap bitmap = BitmapFactory.decodeStream(cr
			// .openInputStream(uri));
			// headportrait.setImageBitmap(bitmap);

			// 保留图片和缩略图
			String headportrait_tbPath = Environment
					.getExternalStorageDirectory().toString()
					+ "/yuanjinImage/" + "headportrait/" + "thumbnail/";
			String time = MyTimeUtils.getTime();
			File out = new File(headportrait_tbPath);
			if (!out.exists()) {
				out.mkdirs();
			}
			headportrait_tbPath = headportrait_tbPath + "headportrait" + time
					+ ".jpg";
			if (absolutePath != "") {
				Bitmap bitmap=MediaUtils.saveMediaThumbnail_small(MediaUtils.MyMedia_Image,
						absolutePath, headportrait_tbPath);
				// 显示缩略图
//				Bitmap bitmap = BitmapFactory.decodeFile(headportrait_tbPath);
				headportrait.setImageBitmap(bitmap);
			} else {
				headportrait.setImageResource(R.drawable.ic_default_userhead);
				Toast.makeText(this, "头像设置未成功", Toast.LENGTH_SHORT).show();
			}

			// 保存头像原图和缩略图的地址
			sp = getSharedPreferences(Yuanjin.YuanjinSharedPreference,
					MODE_PRIVATE);
			// 实例化
			SharedPreferences.Editor editor = sp.edit();
			// 保存原图地址
			editor.putString("headicon_absolutePath", absolutePath);
			// 保存缩略图地址
			editor.putString("headicon_thumbnailPath", headportrait_tbPath);
			// 提交当前的数据
			editor.commit();
		}

	}
}
